'use strict';
const mime = require('mime');

const fs = require('fs/promises');

function statics(){
    return async (ctx, next) => {
        let tmpPath = ctx.request.path;
        console.log(tmpPath);
        let fullPath = __dirname + tmpPath;
        // await next();
        if (tmpPath.startsWith('/statics')) {
            //判断这个文件是否存在，存在则读取，返回给响应，否则返回404
            let stat = await fs.stat(fullPath);
            if (stat.isFile()) {
                ctx.type = mime.getType(fullPath);
                ctx.body = await fs.readFile(fullPath);
            } else {
                ctx.response.status = 404;
            }
        } else {
            await next();
        }
    }
}

module.exports=statics;